package com.ruicar.afs.cloud.apply.common.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.apply.business.mq.config.ArchiveConfig;
import com.ruicar.afs.cloud.apply.business.service.ArchiveMQService;
import com.ruicar.afs.cloud.apply.business.service.TSysParamConfigService;
import com.ruicar.afs.cloud.apply.common.entity.ApplyContractInfo;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.apply.contract.service.ApplyContractInfoService;
import com.ruicar.afs.cloud.common.job.core.biz.model.ReturnT;
import com.ruicar.afs.cloud.common.job.core.handler.annotation.AfsJob;
import com.ruicar.afs.cloud.common.job.core.handler.annotation.AfsJobHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author Ice
 * @date 2020/9/24 16:21
 */
@AfsJob
@AllArgsConstructor
@Slf4j
@Component
public class Task {

    private final TSysParamConfigService tSysParamConfigService;
    private final ApplyContractInfoService applyContractInfoService;
    private final ArchiveMQService archiveMQService;

    @Transactional(rollbackFor = Exception.class)
    @AfsJobHandler(value = "autoInvalidOrder")
    public ReturnT<String> satrt(String params) {
        String days =tSysParamConfigService.getParamValue("loanApply","loanCancel","60");
        //待提交状态
        List<ApplyContractInfo> applyContractInfoList=applyContractInfoService.list(Wrappers.<ApplyContractInfo>query().lambda().eq(ApplyContractInfo::getContractStatus,ApplyConstants.CONTRACT_STATUS_01));
        if(applyContractInfoList.size()>0){
            for (ApplyContractInfo applyContractInfo:applyContractInfoList){
                //取创建时间
                Long day= DateUtil.betweenDay(new Date(),applyContractInfo.getCreateTime(),true);
                if(day>=Integer.parseInt(days)){
                    applyContractInfo.setContractStatus(ApplyConstants.CONTRACT_STATUS_CANCEL);
                    applyContractInfo.setCancelDate(new Date());
                    applyContractInfoService.updateById(applyContractInfo);
                    archiveMQService.archivePushMQ(applyContractInfo.getId().toString(), applyContractInfo.getContractNo(), applyContractInfo.getApplyNo(), ArchiveConfig.wwcontract_cancel);
                }
            }
        }
        //退回状态
        List<ApplyContractInfo> applyContractInfoBack=applyContractInfoService.list(Wrappers.<ApplyContractInfo>query().lambda().eq(ApplyContractInfo::getContractStatus,ApplyConstants.CONTRACT_STATUS_BACK));
        if(applyContractInfoBack.size()>0){
            for (ApplyContractInfo applyContractInfo:applyContractInfoBack){
                //取最近更新时间
                Long day= DateUtil.betweenDay(new Date(),applyContractInfo.getUpdateTime(),true);
                if(day>=Integer.parseInt(days)){
                    applyContractInfo.setContractStatus(ApplyConstants.CONTRACT_STATUS_CANCEL);
                    applyContractInfo.setCancelDate(new Date());
                    applyContractInfoService.updateById(applyContractInfo);
                    archiveMQService.archivePushMQ(applyContractInfo.getId().toString(), applyContractInfo.getContractNo(), applyContractInfo.getApplyNo(), ArchiveConfig.wwcontract_cancel);
                }
            }
        }
        return ReturnT.SUCCESS;
    }
}
